package com.huangpeng.algorithm.dijestra;

import java.util.Arrays;

/**
 * @Author hp
 * @Title 已访问的节点集合
 * @Package
 * @Date 2021/12/31 16:14
 * @Description
 * @Version 1.0
 */
public class VisitedVertex {
    //记录各个顶点是否访问过1表示访问过,0未访问,会动态更新
    public int[] already_arr;
    //每个下标对应的值为前一个顾点下脉，会动态更新
    public int[] pre_visited;
    //记录出发顶点到其他所有项点的地离,比如G为出发顶点，就会记录G到其它顺点的距离，会动态更新，求的最近距离就会存放到dis
    public int[] dis;

    /**
     * len = 节点个数 index = 起始点的下标
     * @param len
     * @param index
     */
    public VisitedVertex(int len, int index) {
        this.already_arr = new int[len];
        this.pre_visited = new int[len];
        this.dis = new int[len];
        //初始化数组
        Arrays.fill(dis,100);
        this.already_arr[index] = 1;
        this.dis[index] = 0;
    }

    /**
     * 判断节点是否被访问
     * @param index
     * @return
     */
    public boolean in(int index){
        return already_arr[index] == 1;
    }

    /**
     * 更新出发顶点和节点的距离
     * @param index
     * @param len
     */
    public void updateDis(int index,int len){
        dis[index] = len;
    }

    /**
     * 更新顶点的前驱节点
     * @param pre
     * @param index
     */
    public void updatePre(int pre,int index){
        pre_visited[pre] = index;
    }

    /**
     * 返回出发节点到dis的距离
     * @param index
     * @return
     */
    public int getDis(int index){
        return dis[index];
    }

    public int updateArr(){
        int min = 100;
        int index = 0;
        for (int i = 0; i < already_arr.length; i++) {
            if(already_arr[i] == 0 && dis[i] < min){
                min = dis[i];
                index = i;
            }
        }
        already_arr[index] = 1;
        return index;
    }

    public void showArr(){
        System.out.println("===============");
        for (int i : already_arr) {
            System.out.print(i + " - ");
        }
        System.out.println();
        for (int i : pre_visited) {
            System.out.print(i + " - ");
        }
        System.out.println();
        for (int i : dis) {
            System.out.print(i + " - ");
        }
        System.out.println();
        //对最短距离进行处理
        char[] vertex = new char[]{'A', 'B', 'C', 'D', 'E', 'F', 'G' };
        int cout = 0;
        for (int i : dis) {
            if(i != 100){
                System.out.print(vertex[cout] + "(" + i + ")  ");
            }else{
                System.out.println("N");
            }
            cout++;
        }
    }
}
